package cn.ctvonline.android.kaopu.activity.frontpage;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.DragEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.ctvonline.android.kaopu.R;
import cn.ctvonline.android.kaopu.base.BaseAppCompatActivity;
import cn.ctvonline.android.kaopu.base.util.ToastUtil;

/**
 * Created by dujian on 15/12/9.
 */
public class ChatActivity extends BaseAppCompatActivity {
    @Bind(R.id.chat_tool_bar)
    Toolbar toolbar;
    private View contentView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        initViews();
    }

    int startX = 0;
    boolean canDrag = false;

    public void initViews() {
        ButterKnife.bind(this);
        toolbar.setTitle("聊天");
        toolbar.setNavigationIcon(R.mipmap.icon_arrow_left);
        toolbar.setOverflowIcon(getResources().getDrawable(R.mipmap.icon_detail));
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.chat_action_detail:
                        ToastUtil.show(getApplicationContext(), "详情");
                        return true;
                }
                return false;
            }
        });
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        contentView = findViewById(android.R.id.content);
        contentView.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                ToastUtil.show(getApplicationContext(), "onDrag");
                return false;
            }
        });
        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i(TAG, "onTouch");
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getRawX();
                        Log.i(TAG, "move the contentView to xDistance=" + startX);
                        if (startX < 16) {
                            canDrag = true;
                        }
                        break;
                    case MotionEvent.ACTION_MOVE: {
                        if (canDrag) {
                            int xDistance = (int) event.getRawX() - startX;
                            Log.i(TAG, "move the contentView to xDistance=" + xDistance);
                            contentView.setTranslationX(xDistance + contentView.getTranslationX());
                        }
                    }
                    case MotionEvent.ACTION_UP: {
                        canDrag = false;
                    }
                    break;
                }
                return false;
            }
        });
    }

    private MenuItem searchItem, settingItem, shareItem;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_chat, menu);
        searchItem = menu.findItem(R.id.chat_action_detail);
        return true;
    }

    @Override
    protected String generatePageName() {
        return "pageName is ChatActivity";
    }
}
